#include <WiFi.h>
const int potPin=A0;
float ph;
float Value=0;
 
String apiKey = "*************"; // API key
const char *ssid = "*************"; //wifi名称和密码
const char *password = "*************";
const char* server = "api.thingspeak.com";
 
WiFiClient client;
 
void setup() {
  Serial.begin(115200);
  pinMode(potPin,INPUT);
  delay(1000);
 
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
  }
  // 打印本地IP
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
}
 
void loop() {
Value= analogRead(potPin);
Serial.print(Value);
Serial.print(" | ");
float voltage=Value*(3.3/4095.0);
ph=(3.3*voltage);
Serial.println(ph);
delay(500);
 
if (client.connect(server, 80))
   {
      String postStr = apiKey;
      postStr += "&field1=";
      postStr += String(ph);
      postStr += "\r\n";
    
      client.print("POST /update HTTP/1.1\n");
      client.print("Host: api.thingspeak.com\n");
      client.print("Connection: close\n");
      client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
      client.print("Content-Type: application/x-www-form-urlencoded\n");
      client.print("Content-Length: ");
      client.print(postStr.length());
      client.print("\n\n");
      client.print(postStr);
    }    
}
